<template>
	<view class="">
		<button @click="selectVideo">选择视频</button>
		<button @click="save">点击</button>
	</view>

</template>

<script>
	import OSS from '@/static/oss/aliyun.js';
	let request = require("../../utils/request.js");
	export default {
		data() {
			return {
				title: 'Hello',
				video: null,
				info: {
					video: null
				}
			}
		},
		onLoad() {

		},
		methods: {
			save() {
				request.requestGet('/user/hello', "真是大怨种了", '', function(res) {

					if (res.code == 200) {
						console.log(res);
					}
				})
			},
			selectVideo() {
				let that = this
				uni.chooseVideo({ //打开选择视频 
					sourceType: ['album'], //直接打开选择视频 ['album','camera'] 就会弹出选框
					success: function(res) {
						that.video = res.tempFilePath; //res.tempFilePath里面就是你选择到的视频
						// console.log(that.video)
						if (res.size > OSS.uploadFileSize) { //OSS.uploadFileSize在aliyun.js里面设置，
							uni.showToast({
								title: '文件大小超过系统上传限制：' + OSS.uploadFileSize,
								icon: 'none',
								duration: 1000
							});
							return;
						}

						var date = new Date;
						var year = date.getFullYear();
						var month = date.getMonth() + 1;
						let filePath = 'xyfw/' + year + "-" + month + "/" + Date.parse(new Date()) + parseInt(
							Math.random() * (100000 - 10000 + 1) + 10000, 10) + ".mp4" //路径自己修改
						that.fileUpload('video', that.video, filePath);
						that.info.video = OSS.host + "/" + filePath;
					}
				});
			},
			fileUpload(type, path, stroeAs) {
				console.log('开始上传')
				let self = this;
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: OSS.host,
						filePath: path,
						name: 'file',
						formData: {
							'key': stroeAs,
							'policy': OSS.policyBase64,
							'OSSAccessKeyId': OSS.accessid,
							'success_action_status': '200',
							'signature': OSS.signature,
						},
						success: (res) => {
							console.log(res)
							uni.showToast({
								title: '上传成功',
								icon: 'success',
								duration: 1000
							});
							console.log(OSS.host + '/' + stroeAs)
							resolve('suc');
						},
						fail: (err) => {
							console.log('upload fail', err);
							uni.showModal({
								content: err.errMsg,
								showCancel: false
							});
							reject('err')
						}
					});

				})
			},

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
